package com.itheima.health.controller;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * @program: itcast_health
 * @create: 2019-10-25 15:47
 * @author: li he
 * @description:
 */
@RestController
@RequestMapping("validateCode")
public class ValidateCodeController {

    @Autowired
    JedisPool jedisPool;

    @RequestMapping("send4Order")
    public Result send4Order(String telephone){

        try {

            //获取随机4位数字
            Integer integer = ValidateCodeUtils.generateValidateCode(4);
            String code = String.valueOf(integer);
            //把当前验证码保存到数据库中
            jedisPool.getResource().setex(RedisConst.SENDTYPE_ORDER + telephone,5*60, code);
            //给用户发送短信

            //SMSUtils.sendShortMessage(telephone,code);

            return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.SEND_VALIDATECODE_FAIL);
        }
    }
    @RequestMapping("send4Login")
    public Result send4Login(String telephone){
        /*
        * 1.生成验证码
        * 2.获取手机号发送短信给用户
        * 3.把验证码存入redis
        * */
        try {
            //1.生成验证码
            String validateCode = String.valueOf(ValidateCodeUtils.generateValidateCode(6));
            //2.获取手机号发送短信给用户
            //SMSUtils.sendShortMessage(telephone,validateCode);
            //3.把验证码存入redis
            jedisPool.getResource().setex(RedisConst.SENDTYPE_LOGIN+telephone,5 * 60,validateCode);

            return new Result(true,MessageConst.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.SEND_VALIDATECODE_FAIL);
        }
    }
}










